//
//  BalloonMarker.swift
//  ChartsDemo-Swift
//
//  Copyright 2015 Daniel Cohen Gindi & Philipp Jahoda
//  A port of MPAndroidChart for iOS
//  Licensed under Apache License 2.0
//
//  https://github.com/danielgindi/Charts
//

import Foundation
import Charts

open class BalloonMarker: MarkerImage
{
    open var color: UIColor
    open var arrowSize = CGSize(width: 15, height: 11)
    open var font: UIFont
    open var textColor: UIColor
    open var insets: UIEdgeInsets
    open var minimumSize = CGSize()
    
    
    
    fileprivate var label: String?
    fileprivate var _labelSize: CGSize = CGSize()
    fileprivate var _paragraphStyle: NSMutableParagraphStyle?
    fileprivate var _drawAttributes = [NSAttributedString.Key : AnyObject]()
    
    public init(color: UIColor, font: UIFont, textColor: UIColor, insets: UIEdgeInsets)
    {
        self.color = color
        self.font = font
        self.textColor = textColor
        self.insets = insets
        
        _paragraphStyle = NSParagraphStyle.default.mutableCopy() as? NSMutableParagraphStyle
        _paragraphStyle?.alignment = .left
        super.init()
    }
    
    open override func offsetForDrawing(atPoint point: CGPoint) -> CGPoint
    {
        guard label != nil else {
            
            return CGPoint(x: 0, y: -10)
            
        }
        let width = label!.size(with: self.font).width + 15
        
        if point.x > (UIScreen.main.bounds.size.width - width / 2) {
            return CGPoint(x: point.x - UIScreen.main.bounds.size.width + width / 2, y: -10)
        }
        
        return CGPoint(x: 0, y: -10)
    }
    
    open override func draw(context: CGContext, point: CGPoint)
    {
        guard let label = label else { return }
        
        let offset = self.offsetForDrawing(atPoint: point)
        
        var rect = CGRect(
            origin: CGPoint(
                x: point.x + offset.x,
                y: point.y + offset.y),
            size: CGSize(width: 15 + self.size.width, height: self.size.height))
        
        rect.origin.x -= size.width / 2.0
        
        rect.origin.y -= size.height / 2
        
        context.saveGState()
        
        context.setFillColor(UIColor.init(white: 0, alpha: 0.5).cgColor)
        
        let path = CGMutablePath.init()
        
        path.addRoundedRect(in: CGRect(origin: rect.origin, size: CGSize(width: rect.size.width, height: rect.size.height * 2 / 3 + 3.0)), cornerWidth: 2, cornerHeight: 2)
        
        context.addPath(path)
        
        if offset.y > 0 {
            rect.origin.y += self.insets.top + arrowSize.height
        } else {
            rect.origin.y += self.insets.top
        }
        
        context.fillPath()
        
        UIGraphicsPushContext(context)
        
        if self.image != nil {
            
            context.draw(self.image!.cgImage!, in: CGRect(origin: CGPoint(x: rect.origin.x + 2, y: rect.origin.y + 2), size: self.image!.size))
            
        }
        
        label.draw(in: CGRect(x: rect.origin.x + 15, y: rect.origin.y, width: rect.width, height: rect.size.height), withAttributes: _drawAttributes)
        
        UIGraphicsPopContext()
        
        context.restoreGState()
    }
    
    open override func refreshContent(entry: ChartDataEntry, highlight: Highlight)
    {
        //        self.image =
        
        var lbl = ""
        
        if let chart = self.chartView {
            
            let color = chart.data?.dataSets[highlight.dataSetIndex].color(atIndex: 0)
            
            switch highlight.dataSetIndex {
            case 0:
                lbl += "计划数据\n"
                break
            case 1:
                lbl += "预算数据\n"
                break
            default:
                lbl += "执行数据\n"
                break
            }
            
            self.image = color?.image(size: CGSize(width: 10, height: 10))
        }
        if let barEntry = entry as? BarChartDataEntry {
            
            //title
            //值
            let set = self.chartView!.data!.dataSets[highlight.dataSetIndex] as! BarChartDataSet
            
            let idx = set.entryIndex(entry: entry)
            
            switch idx{
                case 0:
                    lbl += "I2:"
                    break
                case 1:
                    lbl += "GP:"
                    break
                case 2:
                    lbl += "EC:"
                    break
                case 3:
                    lbl += "P2:"
                    break
                default:
                    lbl += "IC:"
                    break
            }
            lbl += String(barEntry.y)
            setLabel(lbl)
        }
    }
    
    open func setLabel(_ newLabel: String)
    {
        label = newLabel
        
        _drawAttributes.removeAll()
        _drawAttributes[.font] = self.font
        _drawAttributes[.paragraphStyle] = _paragraphStyle
        _drawAttributes[.foregroundColor] = self.textColor
//        _drawAttributes[.a]
        _labelSize = label?.size(withAttributes: _drawAttributes) ?? CGSize.zero
        
        var size = CGSize()
        size.width = _labelSize.width + self.insets.left + self.insets.right
        size.height = _labelSize.height + self.insets.top + self.insets.bottom
        size.width = max(minimumSize.width, size.width)
        size.height = max(minimumSize.height, size.height)
        self.size = size
    }
}

